<?php


namespace app\admin\controller;


use think\facade\View;

class Login extends AdminBase {
    public function initialize() {
        if ($this->isLogin()) {
            $this->redirect(url('index'));
        }
    }

    public function index() {
        return View::fetch();
    }

    public function check() {
        if (!$this->request->isPost()) {
            return show(config("status.error"), "请求方式不对");
        }
        $username = $this->request->param('username', '');
        $password = $this->request->param('password', '');
        $captcha = $this->request->param('captcha', '');

        $data = [
            'username' => $username,
            'password' => $password,
//            'captcha' => $captcha,
        ];
        $validate = new \app\admin\validate\AdminUser();
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        try {
            $adminUserObj = new \app\admin\business\AdminUser();
            $result = $adminUserObj->login($data);
        } catch (\Exception $e) {
            return show(config("status.error"), $e->getMessage());
        }

        if ($result) {
            return show(config('status.success'), '登录成功');
        }

        return show(config('status.error'), $validate->getError());
    }
}